#import <Foundation/NSNotification.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/Foundation.h>	// NSLocalizedString()
#import <UIKit/UIPreferencesTableCell.h>
#import <UIKit/UIPreferencesTextTableCell.h>
#import <UIKit/UIPreferencesControlTableCell.h>
#import <UIKit/UISwitchControl.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UINavigationItem.h>

#import "PreferencesView.h"

@implementation PreferencesView

- (id)initWithFrame: (CGRect)frame parent: (UIApplication *)app
{
	self = [super initWithFrame: frame];
	
	parent = app;
	
	CGSize navBarSize = [UINavigationBar defaultSize];
	
	navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0, 0, frame.size.width, navBarSize.height)];
	[navBar showButtonsWithLeftTitle: @"Back" rightTitle: nil leftBack: YES];
	[navBar setDelegate: parent];
	[navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle: NSLocalizedString(@"Preferences", nil)]];
	
	table = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0, navBarSize.height, frame.size.width, frame.size.height - navBarSize.height)];
	[table setDelegate: self];
	[table setDataSource: self];
	[table reloadData];
	
	[self addSubview: navBar];
	[self addSubview: table];
	
	return self;
}

- (void)dealloc
{
	[navBar release];
	[table release];
}

- (void)save
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject: [hostIPAddress value] forKey: @"host_ip_address"];
	[defaults setInteger: [[hostPort value] intValue] forKey: @"host_port"];
	[defaults setInteger: [[margin value] intValue] forKey: @"margin"];
	[defaults setBool: [moveAbsolute value] forKey: @"move_absolute"];
	[defaults setBool: [gravity value] forKey: @"gravity"];
}

- (NSString *)hostIPAddress
{
	[[NSUserDefaults standardUserDefaults] setObject: [hostIPAddress value] forKey: @"host_ip_address"];
	return [hostIPAddress value];
}

- (int)hostPort
{
	[[NSUserDefaults standardUserDefaults] setInteger: [[hostPort value] intValue] forKey: @"host_port"];
	return [[hostPort value] intValue];
}

- (int)margin
{
	[[NSUserDefaults standardUserDefaults] setInteger: [[margin value] intValue] forKey: @"margin"];
	return [[margin value] intValue];
}

- (BOOL)moveAbsolute
{
	BOOL value = [moveAbsolute value];
	[[NSUserDefaults standardUserDefaults] setBool: value forKey: @"move_absolute"];
	return value;
}

- (BOOL)gravity
{
	BOOL value = [gravity value];
	[[NSUserDefaults standardUserDefaults] setBool: value forKey: @"gravity"];
	return value;
}

- (void)reloadData
{
	[table reloadData];
}

// delegates
- (void)tableRowSelected: (NSNotification *)notification
{
	
}

// data sources
- (int)numberOfGroupsInPreferencesTable: (UIPreferencesTable *)table
{
	return 2;
}

- (int)preferencesTable: (UIPreferencesTable *)table numberOfRowsInGroup: (int)group
{
	switch (group)
	{
		case 0:
			return 2;
		case 1:
			return 2;
	}
	return 0;
}

- (id)preferencesTable: (UIPreferencesTable *)table titleForGroup: (int)group
{
	switch (group)
	{
		case 0:
			return NSLocalizedString(@"Host", nil);
		case 1:
			return NSLocalizedString(@"Mouse", nil);
	}
	
	return nil;
}

- (UIPreferencesTableCell *)preferencesTable: (UIPreferencesTable *)table cellForRow: (int)row inGroup: (int)group
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	
	switch (group)
	{
		case 0:
			switch (row)
			{
				case 0:
					hostIPAddress = [[UIPreferencesTextTableCell alloc] init];
					[hostIPAddress setTitle: NSLocalizedString(@"Host IP Address", nil)];
					[hostIPAddress setValue: [defaults objectForKey: @"host_ip_address"]];
					return [hostIPAddress autorelease];
				
				case 1:
					hostPort = [[UIPreferencesTextTableCell alloc] init];
					[hostPort setTitle: NSLocalizedString(@"Host Port", nil)];
					[hostPort setValue: [NSString stringWithFormat: @"%d", [defaults integerForKey: @"host_port"]]];
					return [hostPort autorelease];
			}
		
		case 1:
			switch (row)
			{
				case 0:
				{
					moveAbsolute = [[UISwitchControl alloc] initWithFrame: CGRectMake(200, 10, 50, 20)];
					UIPreferencesControlTableCell *cell = [[UIPreferencesControlTableCell alloc] init];
					[cell setTitle: NSLocalizedString(@"Move Absolute", nil)];
					[cell setControl: moveAbsolute];
					[moveAbsolute setValue: [defaults boolForKey: @"move_absolute"]];
					return [cell autorelease];
				}
				
				case 1:
				{
					gravity = [[UISwitchControl alloc] initWithFrame: CGRectMake(200, 10, 50, 20)];
					UIPreferencesControlTableCell *cell = [[UIPreferencesControlTableCell alloc] init];
					[cell setTitle: NSLocalizedString(@"Gravity", nil)];
					[cell setControl: gravity];
					[gravity setValue: [defaults boolForKey: @"gravity"]];
					return [cell autorelease];
				}
/*
				case 1:
					margin = [[UIPreferencesTextTableCell alloc] init];
					[margin setTitle: NSLocalizedString(@"Margin", nil)];
					[margin setValue: [NSString stringWithFormat: @"%d", [defaults integerForKey: @"margin"]]];
					return [margin autorelease];
*/
			}
	}
	
	return nil;
}

@end
